package com.tupu.service.impl.dispatch;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.DispatchPlannedOverhaulPlan;
import com.tupu.entity.dto.JxjhDto;
import com.tupu.mapper.dispatch.DadsDdrzMapper;
import com.tupu.mapper.dispatch.DispatchPlannedOverhaulPlanMapper;
import com.tupu.mapper.dispatch.TbJxjhMapper;
import com.tupu.service.dispatch.OverhaulPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class OverhaulPlanImpl extends ServiceImpl<DispatchPlannedOverhaulPlanMapper, DispatchPlannedOverhaulPlan> implements OverhaulPlanService {

    @Autowired(required = false)
    private TbJxjhMapper jxjhMapper;

    @Resource
    private DadsDdrzMapper dadsDdrzMapper;

    @Override
    @DS("dfdz")
    public Page<DispatchPlannedOverhaulPlan> conditionSearch(Long page, Long size, String station_name, String start_time, String end_time, String work_content, String interval_name) throws ParseException {
        // Page page1 = new Page();
        //page1.setCurrent(page);
        // page1.setSize(size);
        QueryWrapper<JxjhDto> wrapper = new QueryWrapper<>();
        Page<JxjhDto> pageInfo = new Page<>(page, size);
        if (StrUtil.isNotEmpty(start_time) && StrUtil.isNotEmpty(end_time)) {
            start_time = start_time + " 00:00:00";
            end_time = end_time + " 23:59:59";
            wrapper.ge("plan_start_time", start_time).le("plan_end_time", end_time);
        }
        if (!StringUtils.isEmpty(station_name)) {
            wrapper.eq("station_name", station_name);
        }
        if (!StringUtils.isEmpty(work_content)) {
            wrapper.like("work_content", work_content);
        }
        if (!StringUtils.isEmpty(interval_name)) {
            wrapper.eq("interval_name", interval_name);
        }
        jxjhMapper.selectPage(pageInfo, wrapper);
        List<DispatchPlannedOverhaulPlan> list = new ArrayList<>();
        Page<DispatchPlannedOverhaulPlan> planList = new Page<>();
        for (JxjhDto jxjhDto : pageInfo.getRecords()) {
            DispatchPlannedOverhaulPlan plan = new DispatchPlannedOverhaulPlan();
            plan.setPlanId(String.valueOf(jxjhDto.getJxjhId()));
            if (!StringUtils.isEmpty(jxjhDto.getProductName())) {
                plan.setProductName(jxjhDto.getProductName());
            }
            if (!StringUtils.isEmpty(jxjhDto.getStationName())) {
                plan.setStationName(jxjhDto.getStationName());
            }
            if (!StringUtils.isEmpty(jxjhDto.getIntervalName())) {
                plan.setIntervalName(jxjhDto.getIntervalName());
            }
            if (!StringUtils.isEmpty(jxjhDto.getPlanStartTime())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = sdf.parse(jxjhDto.getPlanStartTime());
                plan.setPlanStartTime(date);
            }
            if (!StringUtils.isEmpty(jxjhDto.getPlanEndTime())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = sdf.parse(jxjhDto.getPlanEndTime());
                plan.setPlanEndTime(date);
            }
            if (!StringUtils.isEmpty(jxjhDto.getWorkContent())) {
                plan.setWorkContent(jxjhDto.getWorkContent());
            }
            list.add(plan);
        }
        list.sort(Comparator.comparing(DispatchPlannedOverhaulPlan::getPlanStartTime).reversed());
        planList.setRecords(list);
        planList.setTotal(pageInfo.getTotal());
        planList.setSize(pageInfo.getSize());
        planList.setCurrent(pageInfo.getCurrent());
        planList.setPages(pageInfo.getPages());
        //Page<DispatchPlannedOverhaulPlan> planList = dadsDdrzMapper.getDispatchPlannedOverhaulPlan(page1, station_name, start_time, end_time, work_content, interval_name);
        return planList;
    }

    @Override
    public List<String> intervalTime() throws ParseException {
//        List<String> timeList = new ArrayList<>();
//        List<DispatchPlannedOverhaulPlan> list = dispatchPlannedOverhaulPlanMapper.selectList(null);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        for (DispatchPlannedOverhaulPlan plan : list) {
//            String startTime = plan.getPlanStartTime();
//            String endTime = plan.getPlanEndTime();
//            Date start = simpleDateFormat.parse(startTime);
//            Date end = simpleDateFormat.parse(endTime);
//            long time = (end.getTime() - start.getTime()) / (60 * 60 * 1000);
//            StringBuilder sb = new StringBuilder();
//            timeList.add(sb.append(time).append("小时").toString());
//        }
//        return timeList.stream().distinct().sorted().collect(Collectors.toList());
        return null;
    }

    @Override
    @DS("dfdz")
    public List<String> findAllStation() {
        return dadsDdrzMapper.getStationName();
    }

    @Override
    @DS("dfdz")
    public List<String> findIntervalByStationName(String stationName) {
        return dadsDdrzMapper.getIntervalName(stationName);
    }

    /**
     * 定时分析计划操作票，1小时一次
     */
    @Scheduled(cron = "0 0 */1 * * ?")
    public void scheduledCount() {
        log.info("开始计划操作票分析");
        this.analysisJHCZP();
        log.info("计划操作票分析完成");
    }

    @Override
    @DS("dfdz")
    public void analysisJHCZP() {
        List<DispatchPlannedOverhaulPlan> dispatchCZPList = dadsDdrzMapper.getDispatchCZP();
        for (DispatchPlannedOverhaulPlan dispatchPlannedOverhaulPlan : dispatchCZPList) {
            JxjhDto dto = new JxjhDto();
            if (ObjectUtil.isNotEmpty(dispatchPlannedOverhaulPlan.getPlanStartTime()) && ObjectUtil.isNotEmpty(dispatchPlannedOverhaulPlan.getPlanEndTime())) {
                String kssj = DateUtil.formatDateTime(dispatchPlannedOverhaulPlan.getPlanStartTime());
                String jssj = DateUtil.formatDateTime(dispatchPlannedOverhaulPlan.getPlanEndTime());
                dto.setPlanStartTime(kssj);
                dto.setPlanEndTime(jssj);
                //dispatchPlannedOverhaulPlan.setPlanTime(kssj + "-" + jssj);
            }
            if (!StringUtils.isEmpty(dispatchPlannedOverhaulPlan.getProductName())) {
                dto.setProductName(dispatchPlannedOverhaulPlan.getProductName());
            }
            if (!StringUtils.isEmpty(dispatchPlannedOverhaulPlan.getIntervalName())) {
                dto.setIntervalName(dispatchPlannedOverhaulPlan.getIntervalName());
            }
            if (!StringUtils.isEmpty(dispatchPlannedOverhaulPlan.getStationName())) {
                dto.setStationName(dispatchPlannedOverhaulPlan.getStationName());
            }
            if (!StringUtils.isEmpty(dispatchPlannedOverhaulPlan.getWorkContent())) {
                dto.setWorkContent(dispatchPlannedOverhaulPlan.getWorkContent());
            }
            try {
                jxjhMapper.insert(dto);
            } catch (Exception e) {
                jxjhMapper.updateById(dto);
            }
        }

    }
}
